﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Web;
using WLFramework.Data;
using WLFramework.Data.Linq;
using WLFramework.Json;

namespace SampleSite.admin.ajax.grid
{
    /// <summary>
    /// getAllRolesByApp 的摘要说明
    /// </summary>
    public class getAllRolesByApp : AjaxHttpHandler
    {
        public override void OnProcessRequest(HttpContext context)
        {
            int id = context.GetValueByName<int>("id");
            if (id < 0)
                throw new Exception("参数错误");

            using (CommandHelper db = new CommandHelper(CommandMethod.Read))
            {
                if (db.Context.sys_Applications.Where(k => k.A_ID == id).Count() != 1)
                    throw new Exception("恶意参数");

                var roles = from p in db.Context.sys_Roles
                            where p.R_State == 1
                            select p;
                var ar = from p in db.Context.sys_RoleApplication
                         where p.A_ID == id && p.A_State == 1
                         select p;

                var query = from p in roles
                            join r in ar on p.R_ID equals r.R_ID
                                into temp
                            from t in temp.DefaultIfEmpty()
                            select new
                            {
                                id = p.R_ID,
                                cname = p.R_CName,
                                ename = p.R_EName,
                                description = p.R_Description,
                                state = t == null ? 0 : 1
                            };

                context.Response.Write(JSON.Instance.ToJSON(query.ToList()));
            }
        }
    }
}